import json
from datetime import datetime


# json无法序列化自定义类和数组，需要重写序列化·子类
class TimeEncode(json.JSONEncoder):
    def default(self, obj):
        print(
            datetime.strftime(obj, "%Y/%m/%d-%H:%M:%S"),
            type(datetime.strftime(obj, "%Y/%m/%d-%H:%M:%S")),
        )
        return obj.strftime("%Y-%m-%d %H:%M:%S")


class TimeDecode(json.JSONDecoder):
    def decode(self, s):
        ss = json.loads(s)
        return datetime.strptime(ss["data"], "%Y-%m-%d %H:%M:%S")


js = json.dumps({"data": datetime.now()}, cls=TimeEncode)
print(json.loads(js, cls=TimeDecode))
